<?php
/**
 *
 * Decorator for pager links.
 *
 * @category Lux
 *
 * @package Lux_View
 *
 * @author Rodrigo Moraes <rodrigo.moraes@gmail.com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 * @version $Id: PagesList.php 10 2007-08-17 20:14:12Z rodrigo.moraes $
 *
 */

/**
 *
 * Decorator for pager links.
 *
 * @category Lux
 *
 * @package Lux_View
 *
 */
class Lux_View_Helper_Pager_Decorator_PagesList extends Solar_View_Helper
{
    /**
     *
     * Default display configuration. Keys are...
     *
     * `list_type`
     * : (string) Default 'ul', optionally 'ol'.
     *
     * `list_class`
     * : (string) Class for the list block.
     *
     * `item_class`
     * : (string) Class for ordinary items.
     *
     * `first_class`
     * : (string) Class for first page item.
     *
     * `last_class`
     * : (string) Class for last page item.
     *
     * `previous_class`
     * : (string) Class for previous page item.
     *
     * `next_class`
     * : (string) Class for next page item.
     *
     * `current_class`
     * : (string) Class for current page item.
     *
     * @var array
     *
     */
    protected $_Lux_View_Helper_Pager_Decorator_PagesList = array(
        'display' => array(
            'list_type'      => 'ul',
            'list_class'     => 'pagination',
            'item_class'     => null,
            'first_class'    => 'first',
            'last_class'     => 'last',
            'previous_class' => 'previous',
            'next_class'     => 'next',
            'current_class'  => 'current',
        )
    );

    /**
     *
     * Decorator for pager links.
     *
     * @param array $items An array of pager items to be decorated,
     * generated by a pager adapter
     *
     * @param array $display Additional options for building the list.
     *
     * @return string The list of paged links.
     *
     */
    public function pagesList($items, $display = null)
    {
        if($display) {
            $display = array_merge($this->_config['display'], $display);
        } else {
            $display = $this->_config['display'];
        }

        // Set the list type.
        $list_type = $display['list_type'];

        // Set the class attributes.
        $classes = array(
            'list_class',
            'item_class',
            'first_class',
            'last_class',
            'previous_class',
            'next_class',
            'current_class',
        );

        foreach ($classes as $class) {
            if ($display[$class]) {
                $$class = $this->_view->attribs(
                    array('class' => $display[$class])
                );
            } else {
                $$class = false;
            }
        }

        // Build the list.
        $list = array();
        foreach ((array) $items as $key => $info) {
            $class = $info['type'] . '_class';
            if (isset($$class)) {
                // Valid item type.
                $list[] = "    <li{$$class}>\n"
                    . "        {$info['content']}\n"
                    . "    </li>\n";
            }
        }

        // Done!
        return "<$list_type$list_class>\n"
             . implode("\n", $list) . "\n"
             . "</$list_type>\n";
    }
}